/*
 * Copyright (c) 2020 dby4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.supplychain.scm.core.common.util;


import com.supplychain.scm.core.basis.constant.CommonConstants;

import com.supplychain.scm.core.common.constant.IResultCode;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author lengleng
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
	private String requestId;

	@Getter
	@Setter
	private int code;

	@Getter
	@Setter
	private String msg;

	@Getter
	@Setter
	private T data;

	/**
	 * 错误异常属性返回
	 */
	@Getter
	@Setter
	private Object errorData;

	public static <T> R<T> ok() {
		return restResult(null, CommonConstants.SUCCESS, null);
	}

	public static <T> R<T> message(String msg) {
		return restResult(null, CommonConstants.SUCCESS, msg);
	}

	public static <T> R<T> status(boolean status) {
		return restResult(null, status ? CommonConstants.SUCCESS : CommonConstants.FAIL, status ? "操作成功" : "操作失败");
	}

	public static <T> R<T> ok(T data) {
		return restResult(data, CommonConstants.SUCCESS, null);
	}

	public static <T> R<T> ok(T data, String msg) {
		return restResult(data, CommonConstants.SUCCESS, msg);
	}

	public static <T> R<T> failed() {
		return restResult(null, CommonConstants.FAIL, null);
	}

	public static <T> R<T> failed(String msg) {
		return restResult(null, CommonConstants.FAIL, msg);
	}

	public static <T> R<T> failed(String msg, Integer code) {
		return restResult(null, code, msg);
	}

	public static <T> R<T> failed(T data) {
		return restResult(data, CommonConstants.FAIL, null);
	}

	public static <T> R<T> failed(T data, String msg) {
		return restResult(data, CommonConstants.FAIL, msg);
	}

	//被调用服务进入熔断的响应code
	public static <T> R<T> serviceFailed(String msg) {
		return restResult(null, CommonConstants.SERVICE_FAIL, msg);
	}

	public static <T> R<T> serviceFailed() {
		return restResult(null, CommonConstants.SERVICE_FAIL, null);
	}


	public static <T> R<T> serviceFailed(T data, String msg) {
		return restResult(data, CommonConstants.SERVICE_FAIL, msg);
	}

	public static <T> R<T> restResult(T data, int code, String msg) {
		R<T> apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}

	public static <T> R<T> restResult(IResultCode resultCode) {
		R<T> apiResult = new R<>();
		apiResult.setCode(resultCode.getCode());
		apiResult.setMsg(resultCode.getMsg());
		return apiResult;
	}

	public static <T> R<T> restResult(IResultCode resultCode, T data) {
		R<T> apiResult = new R<>();
		apiResult.setCode(resultCode.getCode());
		apiResult.setMsg(resultCode.getMsg());
		apiResult.setData(data);
		return apiResult;
	}

	/**
	 * 错误异常返回
	 * @param errorData 异常数据
	 * @param code 错误码
	 * @param msg 错误说明
	 * @return Object 错误信息
	 */
	public static R errorDate(Object errorData, int code, String msg){
		R<Object> apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setMsg(msg);
		apiResult.setErrorData(errorData);
		return apiResult;
	}
}
